package aak;

import static robocode.util.Utils.normalRelativeAngleDegrees;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import robocode.HitWallEvent;

/**
 * An advance robot to beat the 8 sample robots.
 * 
 * @author Alex Kan
 */

public class SideStep extends Robot {

  /**
   * Constantly turning its gun/radar to scan for enemies.
   */
  public void run() {
    while (true) {
      turnGunRight(100);
    }
  }

  /**
   * A way to scan for the enemy.
   * @param e  an event class
   * @see robocode.Robot#onScannedRobot(robocode.ScannedRobotEvent)
   */
  public void onScannedRobot(ScannedRobotEvent e) {

    // taken from Mathew A. Nelson
    double scan = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getGunHeading()));
    turnGunRight(scan);

    // Fire bullet at different firepower depending on distance away from enemy
    xFire(e.getDistance());

    // Moves the robot closer to the enemy if it's too far
    if (e.getDistance() > 200) {
      turnRight(e.getBearing() - 90);
      ahead(100);

    }
    // Moves the robot away from the enemy if its too close
    else if (e.getDistance() < 200) {
      turnRight(e.getBearing() - 90);
      back(100);
      scan();
    }

  }

  /**
   * A modified version of the smartFire method from "Corners".
   * @param robotDistance  The distance between the enemy and itself.
   */
  public void xFire(double robotDistance) {
    if (robotDistance > 400) {
      doNothing();
    }
    else if (robotDistance > 300) {
      fire(.5);
    }
    else if (robotDistance > 200) {
      fire(1);
    }
    else if (robotDistance > 100) {
      fire(2);
    }
    else if (robotDistance > 50) {
      fire(3);
    }
  }

  /**
   * This helps prevent the robot from getting trapped/stuck on the walls.
   * @param e  an event class
   * @see robocode.Robot#onHitWall(robocode.HitWallEvent)
   */
  public void onHitWall(HitWallEvent e) {
    turnRight(90);
    ahead(200);
  }
}
